<template>
  <div class='container'>
    <div class="app-container">
      <!-- 面试技巧 -->
      <el-form
          :inline="true"
          v-model="articlesRuleForm"
        >

          <el-form-item label="关键字" label-width="80px">
            <el-input
              v-model="articlesRuleForm.keyword"
              class="input-width"
            />
          </el-form-item>

          <el-form-item label="状态" label-width="80px">
            <el-select
            v-model="articlesRuleForm.state"
            placeholder="请选择"
            class="input-width"
          >
            <el-option label="禁用" value="0"></el-option>
            <el-option label="启用" value="1"></el-option>
          </el-select>
          </el-form-item>

          <el-form-item class="btns">
            <el-button @click="clear">清除</el-button>
            <el-button type="primary" @click="search">搜索</el-button>
          </el-form-item>

          <el-form-item class="addSubjects">
            <el-button type="success" @click="onAddSubjects">
              <i class="el-icon-edit"></i>
              新增技巧
            </el-button>
          </el-form-item>
        </el-form>
    </div>
    <div class="total">
      <i class="el-icon-info"><span class="total-font">共{{ articlesList.counts }}条记录</span></i>
    </div>
    <my-table
      :table-props="{
          data: articlesList.items,
          headerCellStyle: getRowClass
        }"
        :columns="[
          { label: '序号', prop: 'id'},
          { label: '文章标题', prop: 'title'},
          { label: '阅读数', prop: 'visits'},
          { label: '录入人', prop: 'username'},
          { label: '录入时间', prop: 'createTime'},
          { label: '状态', prop: 'state'},
          { label: '操作', slotName: 'operators', width: 300}
        ]"
          :pagination-props="{
          pageSize: pageSize,
          pageSizes: [5, 10, 15, 20],
          background: true,
          total: articlesList.counts,
          layout: '->, prev, pager, next, sizes, jumper'
        }"
        :pagination-events="events"
      >
    >
      <template v-slot:operators="scope">
        <el-button type="text" >预览</el-button>
        <el-button type="text" @click="changeState(scope)">
          {{scope.row.state ==='已启用' ? '禁用' : '启用'}}
        </el-button>
        <el-button type="text" @click="updateArticle(scope)" :disabled="scope.row.state =='已启用'">修改</el-button>
        <el-button type="text" @click="deleteInfo(scope)" :disabled="scope.row.state =='已启用'">删除</el-button>
      </template>
    </my-table>

    <!-- 新增标签弹窗 -->
    <el-dialog
      title= "新增文章"
      :visible.sync="addVisible"
      width="800px"
    >
      <el-form
        :model="addArticles"
        label-width="80px"
      >
        <el-form-item label="文章标题" required>
          <el-input
            v-model="addArticles.title"
            placeholder="请输入文章标签"
          />
        </el-form-item>

        <el-form-item label="文章内容" required>
          <quill-editor
              v-model="addArticles.articleBody"
              :options="editorOption"
          />
        </el-form-item>

        <el-form-item label="视频地址" required>
          <el-input
            v-model="addArticles.videoURL"
            placeholder="请输入视频地址"
          />
        </el-form-item>

      </el-form>

      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button type="primary" @click="onAddArticleOk">确定</el-button>
          <el-button @click="onAddArticleClose">取消</el-button>
        </el-col>
      </el-row>

    </el-dialog>

  </div>
</template>

<script>
import { mapState, mapActions } from 'vuex'
import { changeState, remove, add, update } from '@/api/hmmm/articles'

export default {
  data () {
    return {
      editorOption: {
        placeholder: '请在这里输入',
        modules: {
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'],
            [{ list: 'ordered' }, { list: 'bullet' }],
            ['blockquote'],
            ['code-block', 'image', 'link']
          ]
        }
      },
      events: {
        'current-change': this.onCurrentPageChange,
        'size-change': this.onPageSizeChange
      },
      articlesRuleForm: {
        keyword: '',
        state: ''
      },
      addArticles: {
        title: '',
        videoURL: '',
        articleBody: '',
        id: null
      },
      page: 1,
      pageSize: 10,
      addVisible: false,
      update: false
    }
  },
  computed: {
    ...mapState('articles', ['articlesList'])
  },
  created () {
    this.updateArticlesList({
      page: this.page,
      pageSize: this.pageSize
    })
  },
  methods: {
    ...mapActions('articles', [
      'updateArticlesList'
    ]),
    clear () {
      this.articlesRuleForm = {
        keyword: '',
        state: ''
      }
    },
    // 搜索
    search () {
      let f = 0
      const form = {}
      for (const key in this.articlesRuleForm) {
        if (this.articlesRuleForm[key] !== '') {
          f = 1
          form[key] = this.articlesRuleForm[key]
        }
      }
      if (f) {
        this.updateArticlesList({
          ...form,
          page: this.page,
          pageSize: this.pageSize
        })
        console.log(form)
      } else {
        this.updateArticlesList({
          page: this.page,
          pageSize: this.pageSize
        })
      }
    },
    onAddSubjects () {
      this.addVisible = true
    },
    onCurrentPageChange (page) {
      this.page = page
      this.updateArticlesList({
        page: this.page,
        pageSize: this.pageSize
      })
    },
    onPageSizeChange (pageSize) {
      this.pageSize = pageSize
      this.page = 1
      this.updateArticlesList({
        page: this.page,
        pageSize: this.pageSize
      })
    },
    getRowClass ({ rowIndex, columnIndex }) {
      if (rowIndex === 0) {
        return 'background:#f8f8f9; border-bottom: 2px solid #dcdcdc'
      }
    },
    changeState ({ row }) {
      changeState({
        ...row,
        state: row.state === '已启用' ? 0 : 1
      })
      this.updateArticlesList({
        page: this.page,
        pageSize: this.pageSize
      })
    },
    // 删除
    deleteInfo ({ row }) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        remove(row)
          .then(res => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
          })
        this.updateArticlesList({
          page: this.page,
          pageSize: this.pageSize
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    onAddArticleOk () {
      if (this.update) {
        update(this.addArticles)
      } else {
        add(this.addArticles)
      }
      this.onAddArticleClose()
    },
    onAddArticleClose () {
      this.addArticles = {
        title: '',
        videoURL: '',
        articleBody: '',
        id: null
      }
      this.addVisible = false
    },
    updateArticle ({ row }) {
      this.addVisible = true
      this.update = true
      this.addArticles = row
    }

  }
}
</script>

<style scoped lang='sass'>
.container
  background-color: #fff
  padding: 20px
  .app-container
    padding: 0
.addSubjects
  float: right
.total
    font-size: 14px
    background-color: #eee
    padding: 8px
    width: calc(100% - 8px)
    border-radius: 5px
    margin-bottom: 20px
    color: #666
    .total-font
      margin-left: 6px
.input-width
  width: 400px
::v-deep .ql-editor
  height: 200px
</style>
